from django.shortcuts import render

# Create your views here.
from django_filters import constants
from django.http import HttpResponse
from verifications import serializers
from rest_framework.generics import GenericAPIView

from meiduo.libs.yuntongxun.sms import CCP
from rest_framework.views import APIView
from rest_framework.response import Response
from django_redis import get_redis_connection
import random
from celery_tasks.sms.tasks import send_sms_code


# class ImageCodeView(APIView):
#     """
#     图片验证码
#     """
#     def get(self, request, image_code_id):
#         """
#         获取图片验证码
#         :param request:
#         :param image_code_id:
#         :return:
#         """
#         # 生成验证码图片
#
#         text, image = captcha.generate_captcha
#
#         redis_conn = get_redis_connection('"verify_codes"')
#         redis_conn.setex('img_%s' %image_code_id, constants.IMAGE_CODE_REDIS_EXPIRES, text)
#
#         return HttpResponse(image, content_type="images/jpg")


# class SMSCodeView(GenericAPIView):
class SMSCodeView(APIView):
    """
    短信验证
    """
    # serializer_class = serializers.ImageCodeCheckSerializer

    def get(self, request, mobile):
        """
        创建短信验证
        :param request:
        :param mobile:
        :return:
        """

        # # 判断图片验证码是否在６０ｓ内
        # serializers = self.get_serializer(data=request.query_params)
        # serializers.is_vaild(raise_exception=True)

        redis_conn = get_redis_connection('verify')

        # 判断短信６０ｓ
        flag = redis_conn.get('sms_flag_%s' % mobile)
        if flag:
            return Response({'message': 'errors'}, status=401)

        # 生成短信验证码
        sms_code = "%06d" % random.randint(0, 999999)

        # 保存短信与发送记录
        # redis_conn.setex = ("sms_code_%s" % mobile, 300, sms_code)

        # 优化一　　设置ｐｉｐｅｌｉｎｅ（）管道
        pl = redis_conn.pipeline()
        pl.setex("sms_code_%s" % mobile, 300, sms_code)
        pl.setex("sms_flag_%s" % mobile, 60, 1)
        pl.execute()

        # 发送短信验证
        # sms_code_expires =  str(constants.SMS_CODE_REDIS_EXPIRES // 60)
        # ccp = CCP()
        # ccp.send_template_sms(mobile, [sms_code, '5'], 1)

        # sms_code_expires = str(constants.SMS_CODE_REDIS_EXPIRES // 60)
        send_sms_code.delay(mobile, sms_code)

        print(sms_code)

        return Response({"message": "OK"})




# from django.shortcuts import render
#
# from rest_framework.views import APIView
# from rest_framework.response import Response
# from random import randint
# from django_redis import get_redis_connection
# from meiduo.libs.yuntongxun.sms import CCP
# from celery_tasks.sms.tasks import send_sms_code
#
# # Create your views here.
#
#
# class SMSCodeView(APIView):
#
#     def get(self,request,mobile):
#
#         # 判断60s
#         conn = get_redis_connection('verify')
#         flag=conn.get('sms_flag_%s'%mobile)
#         if flag:
#             return Response({'message':'errors'},status=401)
#
#         # 生成短信验证码
#         sms_code='%06d'%randint(0,999999)
#
#         # 保存短信到redis缓存中
#         pl=conn.pipeline()
#         pl.setex('sms_code_%s'%mobile,300,sms_code)
#         pl.setex('sms_flag_%s'%mobile,60,1)
#         pl.execute()
#
#         # 发送短信
#         ccp=CCP()
#         ccp.send_template_sms(mobile,[sms_code,'5'],1)
#         # send_sms_code.delay(mobile,sms_code)
#
#         # 结果返回
#         return Response({'message':'ok'})

